<?php
include("../../advertisement/model.php");

// ---------------------------------
// controller 
// ---------------------------------
// list every advertisements by (type, page_num, list_per_page)
// which define by url GET method
$advertisement_list = model_get_advertisement_list();
array_walk($advertisement_list, "set_edit_page");
array_walk($advertisement_list, "set_delete_page");
$body_html = view_advertisement_list($advertisement_list);

include("../template/layout.php");

// ---------------------------------
// model
// ---------------------------------
function set_edit_page(&$value, $key)
{
	$value["edit_page"] = "edit.php?id=" . $value["id"];
}

function set_delete_page(&$value, $key)
{
	$value["delete_page"] = "delete.php?id=" . $value["id"];
}

// ---------------------------------
// view
// ---------------------------------
function view_advertisement_list($advertisement_list)
{
	$advertisement_list_html = "";
	$advertisement_list_html = "<!--advertisement list start-->\n";
	$advertisement_list_html = $advertisement_list_html . "<div class='advertisement_list'>\n";
	foreach ($advertisement_list as $advertisement)
	{
		$advertisement_list_html = $advertisement_list_html .
		view_advertisement(
		$advertisement["name"],
		$advertisement["click_count"],
		$advertisement["edit_page"],
		$advertisement["delete_page"]
		);
	}
	$advertisement_list_html = $advertisement_list_html . "</div>\n";
	$advertisement_list_html = $advertisement_list_html . "<!--advertisement list end-->\n";
	return $advertisement_list_html;
}

function view_advertisement($name, $click_count, $edit_page, $delete_page)
{
	$advertisement_html =
	sprintf("<table class='advertisement_intro'>
		<tr> <td class='advertisement_name'>%s</td></tr>
		<tr> <td class='advertisement_click_count'>点击数:%d</td> </tr>
		<tr> <td class='advertisement_edit'><a href='%s'>编辑</a></td> </tr>
		<tr> <td class='advertisement_delete'><a href='%s'>删除</a></td> </tr>
		</table>",
		$name,
		$click_count,
		$edit_page,
		$delete_page
	);
	return $advertisement_html;
}
?>
